<template>
	<view class="workerlist">
        <BaseNavBar :isnormarl="true" title="师傅报价" :idx="1"/>
        <block v-if="status">
			<!-- <block v-if="orderList3.length"> -->
				<!-- <scroll-view class="list" scroll-y="true" @scrolltolower="onLoadMore"> -->
					<!-- <view v-for="(item, index) in orderList3" :key="index"> -->
						<workerCard :item3.sync="item"></workerCard>
					<!-- </view>   -->
				<!-- </scroll-view> -->
			<!-- </block> -->
			<!-- <block v-else>
				没有数据
			</block> -->
		</block>
	</view>
</template>

<script>
	import {
		walletData,
		walletDetails,
		rechargeWallet
	} from '../../api'
    import workerCard from './workerCard'
	export default {
		data() {
			return {
                status: true,
				statusbarHeight: 0,
				amount: 0,
				walletData: [],
				totalPay: 0,
				totalIn: 0
			}
		},
        components: {
			workerCard
		},
		mounted() {
			this.statusbarHeight = getApp().globalData.statusBarHeight;
			this.myWallet()
			this.userWelletDetail()
		},
		methods: {
			backHandle() {
				uni.navigateBack();
			},
			// 保留2位小数处理
			priceToFixed(num, digit = 2) {
				let nums = (num || 0).toString().split('.');
				return nums[0] + '.' + (nums[1] || '0').padEnd(digit, "0");
			},
			myWallet() {
				let userId = uni.getStorageSync('userInfo').id
				let data = {
					userId: userId
				}
				walletData(data).then(res => {
					console.log('我的钱包页===', res.data)
					if (res.status == 200) {
						this.walletData = res.data
						this.walletData.forEach(item => {
							if (item.type == '支出') this.totalPay += item.price
							if (item.type == '收入') this.totalIn += item.price
							return (item.price = this.priceToFixed(item.price), this.totalPay, this
								.totalIn)
						})
						this.totalPay = this.priceToFixed(this.totalPay)
						this.totalIn = this.priceToFixed(this.totalIn)
					}
				})
			},
			userWelletDetail() {
				let userId = uni.getStorageSync('userInfo').id
				let data = {
					userId: userId
				}
				walletDetails(data).then(res => {
					console.log('我的钱包详情===', res.data)
					if (res.status == 200) {
						this.amount = this.priceToFixed(res.data.amount)
					}
				})
			},
			//充值
			recharge() {
				//1、获取的code调用后端接口，后端生成一个openId
				let AUTH = uni.getStorageSync("token")
				let data = {
					Authorization: AUTH
				}
				rechargeWallet(data).then(res => {
					if(res.status == 200){
						//2、把openId发送给后端，去调用微信官方的支付接口
						uni.requestPayment({
							provider: 'wxpay', //服务提供商
							appId: res.data.appId,
							timeStamp: res.data.timeStamp, //时间戳，从1970年1月1日00:00:00 至今的秒数，即当前的时间
							nonceStr: res.data.nonceStr, //随机字符串，长度为32个字符以下
							package: res.data.package, //统一下单接口返回的 prepay_id 参数值，提交格式如：prepay_id=***
							signType: res.data.signType, //签名算法，应与后台下单时的值一致
							paySign: res.data.paySign, //签名
							success: (res) => {
								//支付成功的操作
							},
							fail: (err) => {
								// console.log(err);
								uni.showToast({
									icon: 'none',
									title: '支付失败，请重新支付'
								})
							}
						})
					}
				})
				
			}
		}
	}
</script>

<style>
.workerlist {
    width: 100vw;
    height: 100vh;
    background-color: #EFF1F2;
}
</style>